﻿using Kaskelot.Web.Services;
using Stockholm.SOA.Kaskelot.WCF.DataContracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Kaskelot.SP.Services
{
	public class WebContext
	{
        private static log4net.ILog logger = log4net.LogManager.GetLogger("Kaskelot");

		private static readonly string ContextKey = "KaskelotApplicationContext";
		private static readonly string ValCookie = "KaskelotValId";

		public Val Val { get; set; }
		public Person Person { get; set; }
		public AuthenticationLevel AuthLevel { get; set; }

		#region Singleton
		private WebContext()
		{
            logger.Info("Entering WebContext ctor");
			// Person
			string authenticationLevel = HttpContext.Current.Request.Headers["AUTHLEVEL"] ?? string.Empty;
            logger.InfoFormat("Header AUTHLEVEL is {0}", authenticationLevel);

			switch (authenticationLevel)
			{
				case "10":
					AuthLevel = AuthenticationLevel.Password;
					break;
				case "40":
					AuthLevel = AuthenticationLevel.ELeg;
					break;
				default:
					AuthLevel = AuthenticationLevel.Anonymous;
					break;
			}
			string personnummer = HttpContext.Current.Request.Headers["PNR"] ?? string.Empty;
            logger.InfoFormat("Header PNR is {0}", personnummer);
			string email = HttpContext.Current.Request.Headers["MAIL"] ?? string.Empty;
            logger.InfoFormat("Header MAIL is {0}", email);

			// Testing only NOT MEANT TO BE DEPLOYED
			//logger.InfoFormat("TESTING CODE DEPLOYED; REMOVE ASAP");
			//personnummer = "193903059461";

            try
            {
                using (WebService svc = new WebService())
                {
                    this.Val = svc.GetVal(null);
                    if (this.Val != null)
                    {
                        if (!string.IsNullOrEmpty(personnummer))
                        {
                            if (personnummer.Length == 12)
                                personnummer = personnummer.Insert(personnummer.Length - 4, "-");
                            this.Person = svc.GetPersonByPersonnummer(this.Val.Id, personnummer);
                        }
                        else if (!string.IsNullOrEmpty(email))
                        {
                            this.Person = svc.GetPersonByEmail(this.Val.Id, email);
                        }
                    }
                    else
                    {
                        logger.Error("GetVal returned null");
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error calling KaskelotService", ex);
            }

            logger.Info("Exiting WebContext ctor");
		}

		public static WebContext Current
		{
			get
			{
				try
				{
					return new WebContext();
				}
				catch (Exception ex)
				{
                    logger.Error("Error in WebContext.Current", ex);
					return null;
				}
			}
		}
		#endregion
	}
}
